package com.health.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.health.constant.MessageConstant;
import com.health.dao.MemberDao;
import com.health.dao.OrderDao;
import com.health.dao.ReportDao;
import com.health.dao.UserDao;
import com.health.entity.PageResult;
import com.health.entity.QueryPageBean;
import com.health.entity.Result;
import com.health.pojo.Member;
import com.health.pojo.Order;
import com.health.service.MemberService;
import com.health.utils.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @Classname MemberServiceImpl
 * @Description TODO
 * @Version 1.0.0
 * @Date 2023/3/17 14:48
 * @Author RainGrd
 */
@Service(interfaceClass = MemberService.class)
@Transactional
@Slf4j
public class MemberServiceImpl implements MemberService {

    @Autowired
    private MemberDao memberDao;

    @Autowired
    private ReportDao reportDao;

    @Autowired
    private OrderDao orderDao;

    @Autowired
    private UserDao userDao;


    @Override
    public List<Integer> findMemberCountByMonth(List<String> month) {
        Calendar calendar = Calendar.getInstance();
        List<Integer> list = new ArrayList<>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        for (String m : month) {
            String[] split = m.split("\\.");
            //设置年
            calendar.set(Calendar.YEAR, Integer.parseInt(split[0]));
            //设置月
            calendar.set(Calendar.MONTH, Integer.parseInt(split[1]));
            //设置月的最后一天
            calendar.set(Calendar.DAY_OF_MONTH, 0);
            String date = sdf.format(calendar.getTime());
            Integer count = memberDao.findMemberCountBeforeDate(date);
            list.add(count);
        }
        return list;
        /*List<Integer> list = new ArrayList<>();
        for (String m : month) {
//            m = m + "-31";//格式：2019.04.31
            log.info("m:{}", m);
            Integer count = memberDao.findMemberCountBeforeDate(m);
            list.add(count);
        }
        return list;*/
    }

    @Override
    public PageResult queryMemberPage(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        Page<Member> page = memberDao.selectByCondition(queryPageBean.getQueryString());
        log.info("查询出来的会员列表:{}", page.getResult());
        return new PageResult(page.getTotal(), page.getResult());
    }

    @Override
    public Result saveMember(Member member) {
        //设置注册时间
        member.setRegTime(new Date());
        try {
            //根据手机号查询会员是否存在
            Member memberDaoByTelephone = memberDao.findByTelephone(member.getPhoneNumber());
            if (memberDaoByTelephone != null) {
                return Result.error("该手机号码已经注册过会员了");
            }
           /* //新增用户，根据用户名查找
            userDao.selectUserByUserName(member.getName());*/
            int i = memberDao.insertMember(member);
            return i > 0 ? Result.success(MessageConstant.ADD_MEMBER_SUCCESS) : Result.error(MessageConstant.ADD_MEMBER_FAIL);
        } catch (Exception e) {
            log.error(e.getMessage());
            return Result.error(MessageConstant.ADD_MEMBER_FAIL);//新增失败
        }
    }

    @Override
    public int editMember(Member member) {
        try {
            member.setBirthday(DateUtils.parseString2Date(String.valueOf(member.getBirthday())));
            member.setRegTime(DateUtils.parseString2Date(String.valueOf(member.getRegTime())));
        } catch (Exception e) {
            log.info(e.getMessage());
        }

        return memberDao.updateMember(member);
    }

    @Override
    public Result deleteMemberById(Integer id) {
        log.info("删除会员的Id:{}", id);
        try {
            //根据此Id查询预约
            Order order = orderDao.selectOrderByMemberId(id);
            if (order != null) {
                return Result.error(MessageConstant.DELETE_REPORT_FLAG);
            }
            int i = memberDao.deleteMemberById(id);
            return i > 0 ? Result.success(MessageConstant.DELETE_MEMBER_SUCCESS) : Result.error(MessageConstant.DELETE_MEMBER_FAIL);
        } catch (Exception e) {
            log.error(e.getMessage());
            return Result.error(MessageConstant.DELETE_MEMBER_FAIL);//新增失败
        }

    }
}
